KindEditor.ready(function(K) {
	window.editor = K.create('#intro',{
		allowImageUpload:false,
		allowFlashUpload:false,
		allowMediaUpload:false,
		allowFileUpload:false,
		items:['source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste','plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright','justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript','superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/','formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold','italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image','flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak','anchor', 'link', 'unlink']
	});
});
$(document).ready(function(){
	$("#url").focus().blur(function(){
		if(check_url())
		{
			$.getJSON('/product/getinfo',{url:$(this).val()},function(data){
				product_info = $("#product_info");
				product_info.empty();
				if(data.status)
				{
					dlsite  = $("<dl />").addClass('dl-horizontal').appendTo(product_info);
					$("<dt />").html("网站：").appendTo(dlsite);
					$("<dd />").html(data.extra.site_name).appendTo(dlsite);
					dltitle = $("<dl />").addClass('dl-horizontal').appendTo(product_info);
					$("<dt />").html("商品名称：").appendTo(dltitle);
					$("<dd />").html(data.extra.item_title).appendTo(dltitle);
					dlprice = $("<dl />").addClass('dl-horizontal').appendTo(product_info);
					$("<dt />").html("商品价格：").appendTo(dlprice);
					$("<dd />").html(data.extra.item_price).appendTo(dlprice);
					if($("#title").val()=='')
					{
						$("#title").val("【" + data.extra.site_name + "】" + data.extra.item_title);
					}
				}
				else
				{
					product_info.html("Oops，系统没有获取到此网页的信息。");
				}
			});
		}
	});
	$("#submiting").click(function(event) {
		event.preventDefault();
		window.editor.sync('intro');
		if(!check_title())
		{
			alert("请简单地填写一下标题，谢谢！");
			return false;
		}
		if(!check_url())
		{
			alert("请填写优惠信息或商品的网址。");
			return false;
		}
		$.post("/product/submit",
			$("#product_submit").serialize(),
			function(data){
				if(data.status)
				{
					alert("提交成功，请等待我们的审核。");
					window.location.href = "/";
				}
				else
				{
					alert(data.msg);
				}
			},
			"json"
		);
	});
});
check_title=function()
{
	if($("#title").val().length <= 3)
	{
		return false;
	}
	return true;
}
check_url=function()
{
	if(!$("#url").val().match(/^https?:\/\//i))
	{
		return false;
	}
	return true;
}